<?php
/**
 * @uses 	江油市第二人民医院HIS前置机中间件
 * @author 	sunny
 * @since  	2016-05-19 
 */

namespace Jiangyou2\Controller;

class Jiangyou2ControlController 
{

	static $URL_API = "http://61.157.98.56:8098/Wx_WebService.asmx/"; 	//测试地址
    //static $URL_API = "http://61.157.98.56:8098/Wx_WebService.asmx";
    //时间转换
    public $stringToTime = array("上午班"=>"am","下午班"=>"pm");
    public $shift 	= array(1=>'上午班',2=>'下午班',3=>'夜班',4=>'昼夜',5=>'白天',6=>'前夜',7=>'后夜',9=>'其他');
    private $token 	= "7daf7603429012aeae417fa9beed9c12";
    private $pay_channel = "000001";
   	
    function __construct()
    {
        if (!method_exists($this , ACTION_NAME)) {
            $function = ACTION_NAME;
            A('Receive')->$function();  
        }
    }

    /**
     * @uses 	门诊就诊科室查询
     * @author  sunny
     * @since 	2016-05-23
     * @param 	hospitalId 	医院ID
     * @param 	date 		日期
     * @param   timeStr 	时间戳
     */
    public function dutyDept(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        $date = $_POST['date'];
        $timeStr = $_POST['timeStr'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        //当前挂号科室类型区分
        $now_time=date('Y-m-d',time());
        if(strtotime($now_time)==strtotime($date)){
            $IsAppt=1;
        }
        else{
            $IsAppt=0;
        }

        $params = array();
        $params['IsAppt'] = $IsAppt;
        $params['opClassId'] = '';
        $result= $this->Post(self::$URL_API.'getOutDepaList',__FUNCTION__,$params);
        //返回数据
        $data = array();
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";
        if(!$result['data']){
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = isset($result['msg'])?$result['msg']:"访问异常！code:201";
            echo json_encode($returnArr);exit;
        }else{//格式化为标准接口模式
            foreach ($result['data']as $k=>$v){
                $data[$v['DeptId']]['deptDutyId'] = $v['DeptId'];
                $data[$v['DeptId']]['deptId'] = $v['DeptId'];
                $data[$v['DeptId']]['title'] = $v['DeptName'];
            }
        }
        $returnArr['data'] = $data;
        echo json_encode($returnArr) ;
    }
    
    /**
     * @uses 	门诊就诊科室医生查询
     * @param 	hospitalId 	医院ID d
     * @param   deptId 		科室ID
     * @param 	date 		日期
     * @param   timeStr 	时间戳
     * @param 	c_page 		页码
     * @param 	cardId 		就诊卡号 		
     * @author 	sunny
     * @since 	2016-05-23
     */
    public function dutyDoctor(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }

        $hospitalId = $_POST['hospitalId'];
        $deptId = $_POST['deptId'];
        $date = $_POST['date'];
        $timeStr = $_POST['timeStr'];
        $c_page = round($_POST['c_page']);
        $cardId= $_POST['cardId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['dept_code'] = $deptId;
        $data['c_page'] = $c_page;
        //当前挂号科室类型区分
        $now_time=date('Y-m-d',time());
        if(strtotime($now_time)==strtotime($date)){
            $IsAppt=1;
        }
        else{
            $IsAppt=0;
        }
        $params = array();
        $params['Patientid'] = $cardId;
        $params['OpclassId'] = '';
        $params['DeptId'] = $deptId;
        $params['DoctID'] = '';
        $params['startDate'] = $date;
        $params['endDate'] = $date;
        $params['IsAppt'] = $IsAppt;
        $result= $this->Post(self::$URL_API.'DoctRBList',__FUNCTION__,$params);
        //返回数据
        $data = array();
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";
    
        if(!$result){
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = isset($result['message'])?$result['message']:"访问异常！code:201";
            echo json_encode($returnArr);exit;
        }else{//格式化为标准接口模式
            foreach ($result['data'] as $k=>$v){
                $data[]=array(
                        'name'=>$v['Doctname'],
                        'docId'=>$v['Doctid'],
                        'profession'=>$v['RegType'],
                        'deptName'=>$v['DeptName'],
                        'deptHisId'=>$v['DeptId'],
                        'rest'=>0
                    );
            }
        }
        $returnArr['data'] = $data;
        $returnArr['haveRest'] = "false";
        echo json_encode($returnArr) ;
    }
    
    /**
     * @uses 	获取号源
     * @param 	hospitalId 医院ID
     	&deptId=科室ID&date=日期&timeStr=时间戳
     * @author 	sunny
     * @since 	2016-5-23
     */
    public function getDoctorSource($hospitalId,$docHisId,$deptHisId, $date,$cardId){
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['doctor_code'] = $docHisId;
        $data['treat_date'] = $date;
        $data['dept_code'] = $deptHisId;
        $time = $_POST['time'];
        $now_time=date('Y-m-d',time());
        if(strtotime($now_time)==strtotime($date)){
            $IsAppt=1;
        }
        else{
            $IsAppt=0;
        }

        $params = array();
        $params['Patientid'] = $cardId;
        $params['OpclassId'] = '';
        $params['DeptId'] = '';
        $params['DoctID'] = $docHisId;
        $params['startDate'] = $date;
        $params['endDate'] = $date;
        $params['IsAppt'] = $IsAppt;
        $result= $this->Post(self::$URL_API.'DoctRBList',__FUNCTION__,$params);
        //返回数据
        $data = array();
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";  
        if(!$result){
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = isset($result['message'])?$result['message']:"访问异常！code:201";
            echo json_encode($returnArr);exit;
        }else{//格式化为标准接口模式
            foreach ($result['data'] as $k=>$v){
                 $newData[]=array(
                        'RBASId'=>$v['RegisterId'],
                        'range'=> $time,
                        'date'=>$v['RegisterDate'],
                        'treat_date'=>$v['RegisterDate'],
                        'treat_time'=>'09:00:00',
                        'fee'=>$v['SumFee'],
                        'rest'=>$v['Remain']
                    ); 
            }
        }

        $returnArr['data']['duty'] = $newData;
        echo json_encode($returnArr);
    }
    

    
    /**
     * @uses 	预约挂号提交(挂号前检查及锁号)
     * @author	sunny
     * @since 	2016-5-25
     */
    function registration(){
        //不执行锁号流程
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = '操作成功';
        $returnArr['data']['apptId'] = $_POST['RbasId'];
        $returnArr['data']['fee'] = $_POST['fee'];
        echo json_encode($returnArr);
        exit;
    }

    /**
     * @uses 	支付后进行挂号
     * @param 	cardNo=卡号&dutyDocId=号源号
     * @author 	sunny
     * @since  	2016-5-25
     */
    public function registrationFetch(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
                echo "签名不正确";exit;
        }
        $date = $_POST['date']; 
        $ScheduleCd = $_POST['ApptId'];       
        $params = array();
        $params['Patientid'] = $_POST['cardNo'];
        $params['RegisterId'] = $ScheduleCd;
        $result= $this->Post(self::$URL_API.'opAvisit',__FUNCTION__,$params);
        $result=$result['data'][0];
        if(!$result){
            exit;
        }
        $year = substr($date, 0,4);
        $month = substr($date, 5,2);
        $day = substr($date, 8,2);
        $data['RegYear'] = $year;
        $data['RegMonth'] = $month;
        $data['RegDay'] = $day;
        $data['date'] = $date;
        $data['apptId'] = $ScheduleCd;
        $data['queueNo'] = $result['AvisitId'];
        $data['address'] = $result['address'];
        $data['regType'] = "普通门诊";
        $data['LocInfo'] = "江油市第二人民医院";
        $data['fee'] = $result['Amount'];
        $data['invoiceNo'] = $result['AvisitId'];
        $data['TimeInfo'] = "请于".$year."年".$month."月".$day."日".$result['TimeInfo']."前来就诊";
        if ($result['RespCode'] == 0) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = "";
            $returnArr['data'] = $data;
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['RespMessage'];
            $returnArr['data'] = array();
        }
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";
        $returnArr['data'] = $data;
        echo json_encode($returnArr);
    }

	/**
	 * @uses 	获取平台认识信息
	 */
	public function getFormIdentify(){
		$data['ins_code'] = "000000";
		$data['password'] = "ef2492888e824c28b5cf6ad0dae9d97c";
		$data['channel'] = "000001";
	
		$params = array();
		$params['bs_code'] = 'T_00002';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
				echo "<pre>";print_r($params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	获取系统介绍
	 */
	public function getSystemIntroduce(){
		$params = array();
		$params['bs_code'] = 'S_00002';
		$params['data'] = '';
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		print_r($r);
	}
	
	/**
	 * @uses 	用户注册
	 */
	public function userRegster(){
		$data['identyId'] = "120103199801011934";
		$data['password'] = "123456";
		$data['name'] = "test123";
		$data['phone'] = "17713685654";
		$data['cityCode'] = "510100";
		
		
		$params = array();
		$params['bs_code'] = 'T_00010';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
	/**
	 * @uses 	用户登陆
	 */
	public function userLogin(){
		$data['identyId'] = "120103199801011934";
		$data['password'] = "123456";
		$data['token'] = "9f6c1330534046d4b651a84f6d84748f";
	
	
		$params = array();
		$params['bs_code'] = 'T_00020';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	用户基本信息查询
	 */
	public function userBaseInfo(){
		$data['identy_id'] = "120103199801011934";
	
		$params = array();
		$params['bs_code'] = 'S_00012';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	医院介绍-栏目查询
	 */
	public function hosColumnQuery(){
		$data['identy_id'] = "120103199801011934";
		$data['hos_code'] = "512000000001";
		$params = array();
		$params['bs_code'] = 'S_00015';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
	/**
	 * @uses 	医院介绍-栏目内容查询 nodata
	 */
	public function hosColumnIntro(){
		$data['hos_code'] = "512000000001";
		$data['title_code'] = "2010";
		$params = array();
		$params['bs_code'] = 'S_00016';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	医疗机构查询
	 */
	public function hosInstitutionQuery(){
		$data['cityCode'] = "512000";
		$params = array();
		$params['bs_code'] = 'S_00100';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
	/**
	 * @uses 	医疗机构简介查询
	 */
	public function hosInstitutionIntro(){
		$data['hos_code'] = "510000000001";
		$params = array();
		$params['bs_code'] = 'S_00101';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	医疗机构院区信息查询
	 */
	public function hosInstitutionArea(){
		$data['hos_code'] = "510000000001";
		$params = array();
		$params['bs_code'] = 'S_00102';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
    /**
     * @uses 	医疗机构科室查询
     */
    public function hosInstitutionClass(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $params = array();
        $params['bs_code'] = 'S_00103';
        $params['reqData'] = json_encode($data);    
        $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
        $result = json_decode($r,true);

        //返回数据
        $data = array();
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";

        if($result['result']!=1){
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = isset($result['message'])?$result['message']:"访问异常！code:201";
            echo json_encode($returnArr);exit;
        }else{//格式化为标准接口模式
            $i = 0;
            foreach ($result['data_list'] as $k => $v){
                $data[$i]['deptName'] = $v['dept_name'];
                $data[$i]['deptHisId'] = $v['dept_code'];
                $data[$i]['deptIntro'] = $v['intro'];
                $data[$i]['deptIntroUrl'] = $v['intro_url'];
                $data[$i]['deptAddress'] = $v['address'];
                $data[$i]['deptPosition'] = $v['position'];
                $data[$i]['deptTel'] = $v['tel'];
                $data[$i]['rest'] = 0;
                $i++;
            }
        }
        $returnArr['data'] = $data;
        $returnArr['haveRest'] = "false";
        echo json_encode($returnArr);
    }
	
	/**
	 * @uses 	医疗机构科室清单查询
	 */
	public function hosInstitutionClassList(){
		$data['hos_code'] = "512000000001";
		$params = array();
		$params['bs_code'] = 'S_00108';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	医疗机构科室简介查询
	 */
	public function hosInstitutionClassIntro(){
		$data['hos_code'] = "512000000001";
		$data['dept_code'] = "e49604f829f3c9fd5d17";
		$params = array();
		$params['bs_code'] = 'S_00108';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
	

	/**
	 * @uses 	检查医院是否已配置
	 * @param int $hospitalId
	 */
	public function checkHospital($hospitalId){
// 	    $returnArr = array();
// 	    if(!isset($this->hospitalCode[$hospitalId])){
// 	        $returnArr['status'] = 2;
// 	        $returnArr['errorMsg'] = "没有找到你需要的医院";
// 	        echo json_encode($returnArr);exit;
// 	    }
	    return "512000000001";
	}
	
	
	
	
	/**
	 * @uses 	门诊医生介绍
	 */
	public function getDoctorIntroduce(){
		$data['hos_code'] = "512000000001";
		$data['doctor_code'] = "0308";
	
		$params = array();
		$params['bs_code'] = 'S_01032';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($r);
	}
	
	
	/**
	 * @uses 	门诊挂号号源查询
	 */
	
	
	/**
	 * @uses 	门诊医生出诊时间查询
	 */
	public function getDoctorWorktime(){
		$data['hos_code'] = "512000000001";
		$data['doctor_code'] = "0308";
		$data['dept_code'] = "mzks0026";
	
		$params = array();
		$params['bs_code'] = 'S_01031';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
// 		echo "<pre>";print_r($params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	生成挂号订单
	 */
	public function generateRegOrder($hos_code,$doctor_code,$dept_code,$user_name,$treat_bc,$treat_num,$identy_id,$treat_date,$treat_time,$phone){
		$data['hos_code'] = $hos_code;
		$data['doctor_code'] = $doctor_code;
		$data['dept_code'] = $dept_code;
		$data['user_name'] = $user_name;
		$data['treat_bc'] = $treat_bc;
		$data['treat_num'] = (string)$treat_num;
		$data['identy_id'] = $identy_id;
		$data['treat_date'] = $treat_date;
		$data['treat_time'] = $treat_time.":00";
		$data['phone'] = $phone;
		$data['agent_identy_id'] = $identy_id;
		$data['token'] = "94bc4e8169fd4a54839866f6a7d57598";
		$data['pay_channel'] = "000001";
	
		$params = array();
		$params['bs_code'] = 'T_01011';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
// 		print_r($params);
		return $r;
		
		//{"result":"1","message":"下单成功","data":{"fee1":5,"order_period":10,"order_number1":"021116011300203"}}
	}
	
	/**
	 * @uses 	订单查询
	 */
	public function queryOrder(){
		$data['state_type'] = "";
		$data['identy_id'] = "120103199801011934";
// 		$data['pay_channel'] = "000001";
	
		$params = array();
		$params['bs_code'] = 'S_02010';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($params);
		echo "<pre>";print_r($r);
	}
	
	

	/**
	 * @uses 	订单可支付查询
	 */
	public function canPayOrder($hos_code,$identy_id,$order_number){
		$data['order_number'] = $order_number;
		$data['hos_code'] = $hos_code;
		$data['identy_id'] = $identy_id;
	
		$params = array();
		$params['bs_code'] = 'S_02011';
		$params['reqData'] = json_encode($data);
		
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
// 		echo "<pre>";print_r($params);
// 		echo "<pre>";print_r($r);
		return $r;


	}
	
	/**
	 * @uses 	查询支付方式
	 */
	public function queryPayType(){
		$data['hos_code'] = "512000000001";
		$data['identy_id'] = "120103199801011934";
	
		$params = array();
		$params['bs_code'] = 'S_00013';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	订单支付
	 */
	public function orderPay(){
		$data['order_number'] = "021116011300203";
		$data['identy_id'] = "120103199801011934";
	
		$params = array();
		$params['bs_code'] = 'T_02030';
		$params['reqData'] = json_encode($data);
		$r = $this->Post(self::$URL_API,__FUNCTION__,$params);
		echo "<pre>";print_r($params);
		echo "<pre>";print_r($r);
	}
	
	/**
	 * @uses 	订单提交
	 */
	public function orderSubmit($order_number,$identy_id){
	    $data['order_number'] = $order_number;
	    $data['identy_id'] = $identy_id;
	    $data['token'] = $this->token;
	
	    $params = array();
	    $params['bs_code'] = 'T_02031';
	    $params['reqData'] = json_encode($data);
	    $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
// 	    echo "<pre>";print_r($params);
// 	    echo "<pre>";print_r($r);
        return $r;
	}
	
    /**
     * @uses 	门诊缴费查询
     * @param 	:hospitalId＝医院id&IDCardNo=就诊卡号
     * @author 	sunny
     * @since 	2016-5-26
     */
	public function doctorAdvice(){
	    $sign = $this->middelEnCodeSign($_POST);
	    if($sign!=$_POST['sign']){
	        echo "签名不正确";exit;
	    }
	    $hospitalId = $_POST['hospitalId'];
	    //组装进行访问接口
	    $identy_id = $_POST['cardId'];;
	    $params = array();
	    $params['VisitId'] = '';
        $params['BeginDate'] = '';
        $params['endDate'] = '';
        $params['patientId'] = $identy_id;
        $params['BillNO'] = '';
        $result= $this->Post(self::$URL_API.'getChargeList',__FUNCTION__,$params);
	    //返回数据
	    $returnArr['status'] = 1;
	    $returnArr['errorMsg'] = "";
	    if($result['data']) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = $result['msg'];
            //格式化缴费列表
            if(isset($result['data']) && !empty($result['data'])){
                foreach ($result['data'] as $v){
                    $type=0;
                    if($v['BillType']==1 || $v['BillType']==2)$type=1;
                    if(in_array($v['BillNO'],$data_recipeId)){
                        $data['payList'][$v['BillNO']]['fee']=$data['payList'][$v['BillNO']]['fee']+$v['BillSum'];
                    }
                    else{
                        $data['payList'][$v['BillNO']]=array(
                                'recipeId'=>$v['BillNO'],
                                'orderNumber'=>$v['VisitID'],
                                'fee'=>$v['BillSum'],
                                'payDate'=>date("Y-m-d", strtotime($v['BillTime'])),
                                'type'=>$type
                            );
                        $data_recipeId[]=$v['BillNO'];
                    }
                }
            }
            $returnArr['data'] = $data;
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['msg'];
            $returnArr['data'] = array();
        }

	    echo json_encode($returnArr);
	}
	
	
    /**
     * @uses:生成交费订单
     * @param :hospitalId＝医院id&orderNum=挂号流水号&recipeId=处方号
     * @author: sunny
     * @since 2016-5-27
     */
	public function preGeneratePayOrder(){
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        $visitid=$_POST['orderNum'];
        $billno=$_POST['recipeId'];


        //获取基础信息
        $params = array();
        $params['VisitId'] = $visitid;
        $params['BeginDate'] = '';
        $params['endDate'] = '';
        $params['patientId'] = '';
        $params['BillNO'] = '';
        $result2= $this->Post(self::$URL_API.'getChargeList',__FUNCTION__,$params);
        if($result2['data']){
            foreach ($result2['data'] as $v){
                if($v['BillNO']==$billno && $v['PayFlag']==0){
                        $data['Doctid']=$v['Doctid'];
                        $data['doctorName']=$v['Doctname'];
                        $data['execLoc']='';
                        $data['PayFlag']=$v['PayFlag'];
                        $data['payDate']=$v['BillTime'];
                        $data['recipeId']=$billno;
                }
             }
        }

        if($data['PayFlag']){
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = '无效订单';
            echo json_encode($returnArr);
            exit;
        }

        //医生详细信息
        $params = array();
        $params['Doctid'] = $data['Doctid'];
        $params['deptId'] = '';
        $result3= $this->Post(self::$URL_API.'getDocInfo',__FUNCTION__,$params);
        $docter=current(current($result3));
        $data['deptName']=$result3['Doctgender'];

        $params = array();
        $params['VisitId'] = $visitid;
        $params['BillNO'] = $billno;
        //获取扩展信息
        $result= $this->Post(self::$URL_API.'getChargeInfo',__FUNCTION__,$params);
        if ($result['data']) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = $result['RespMessage'];
            //格式化缴费列表
            if(isset($result['data']) && !empty($result['data'])){
                $fee=0;
                foreach ($result['data'] as $v){
                    $c_data['items']=array();
                    $c_data['items'][]=array(
                            'itemName'=>$v['ITEM_NAME'],
                            'Specs'=>$v['SPECS'],
                            'unit'=>doubleval($v['PRICE_UNIT']),
                            'fee'=>doubleval($v['UNIT_PRICE']),
                            'quantity'=>doubleval($v['QTY']),
                            'Prescription'=>$billno
                        );
                    $c_data['cateFee']=doubleval($v['OWN_COST']);
                    $fee=$fee+doubleval($v['OWN_COST']);
                    $n_data[]=$c_data;
                }
                $data['orderNumber']=$visitid;
                $data['fee']=$fee;
                $data['categories']=$n_data;
            }
            $returnArr['data'] = $data;
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['RespMessage'];
            $returnArr['data'] = array();
        }
       
        echo json_encode($returnArr);
        exit;
    }
	
	//缴费生成订单
	public function GeneratePayOrder($hos_code,$identy_id,$totolFee,$charge_list){
	    $data['hos_code'] = $hos_code;
	    $data['identy_id'] = $identy_id;
	    $data['agent_identy_id'] = $identy_id;
	    $data['pay_type'] = "55";
	    $data['pay_mode'] = "20";
	    $data['pay_fee'] = (string)$totolFee;
	    $data['version'] = "2.2.0";
	    $data['pay_channel'] = $this->pay_channel;
	    $data['charge_list'] = $charge_list;
	    $data['token'] = $this->token;
	    
	    $params = array();
	    $params['bs_code'] = 'T_02042';
	    $params['reqData'] = json_encode($data);
	    $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
	    return $r;
// 	    echo "<pre>";print_r($params);
// 	    echo "<pre>";print_r($r);
	}
	
	/**
    * @uses :门诊支付提交
    * @param:hospitalId＝医院id&orderNumber=门诊唯一标识符&cardNo=用户卡号&transaction_id&billno=处方号
    * @author:sunny
    * @since:2016-5-27
    */
	public function outPatientPay(){
	    $sign = $this->middelEnCodeSign($_POST);
	    if($sign!=$_POST['sign']){
	        echo "签名不正确";exit;
	    }
	    $hospitalId = $_POST['hospitalId'];
	    $order_number= $_POST['orderNumber'];
        $cardNo= $_POST['cardNo'];
        $transaction_id= $_POST['transaction_id'];
        $fee= $_POST['fee'];
        $medicineRetrun=$_POST['medicineRetrun'];
        $medicineRetrun=json_decode($medicineRetrun,true);
        $billno=$medicineRetrun['recipeId'];
        //获取基础信息
        $params = array();
        $params['VisitId'] = $order_number;
        $params['billno'] = $billno;
        $params['patientId'] = $cardNo;
        $params['amount'] = $fee;
        $params['termid'] = '';
        $params['optId'] = '';
        $params['paySerNo'] = $transaction_id;
        $params['payType'] = 1;
        $result= $this->Post(self::$URL_API.'doPay',__FUNCTION__,$params);
        //返回数据
        $data = array();
        $returnArr['status'] = 1;
        $returnArr['errorMsg'] = "";
        if($result['data']){
            //已支付状态
            if($data['ret']==1){
                $returnArr['data'] = array();
                echo json_encode($returnArr);exit;
            }else{
                $returnArr['status'] = 2;
                $returnArr['errorMsg'] = $result['msg'];
                echo json_encode($returnArr);exit;
            }
        }
	    
	}
	
	
	public function userCardQuery(){
	    $data['hos_code'] = "512000000001";
	    $data['identy_id'] = "120103199801011934";
	    $data['token'] = "94bc4e8169fd4a54839866f6a7d57598";
	
	    $params = array();
	    $params['bs_code'] = 'S_06030';
	    $params['reqData'] = json_encode($data);
	    $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
	    echo "<pre>";print_r($params);
	    echo "<pre>";print_r($r);
	}

    /**
    * @uses 	检查医院对应签名
    * @param	hospitalId=医院id
    * @author	sunny
    * @since 	2016-5-19
    */
	function middelEnCodeSign($params) {
        ksort($params);
        $preString = "";
        $time = $params['time'];
        $hospitalId = $params['hospitalId'];
        foreach ($params as $k => $v) {
            if ($k == 'time' || $k == 'sign' || !$v)
                continue;
            $preString .= $k . "=" . $v;
        }
        //$keyDb = M("cloud_key");
        //查询医院对应的KEY
        // $keyString = $keyDb->where(array("hospitalId" => $hospitalId))->getField("keyString");
        // if (!$keyString) {
        //     echo "没有权限访问接口";
        //     exit;
        // }
        //加密
        $keyString 	= $this->token;
        return sha1($preString . $keyString . $time);
     }
        

    /**
     * @uses 	检查报告列表
     * @param 	identy_id=卡号
     * @author 	sunny
     * @since  	2016-5-30
     */
	function myInspectReport(){
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        
        //获取基础信息
        $params = array();
        //$params['patientId'] = $_POST['cardNo'];
        $params['CheckId'] = '';
        $params['BeginDate'] = '';
        $params['EndDate'] = '';
        $result= $this->Post(self::$URL_API.'getLis',__FUNCTION__,$params);

        if ($result['data']) {
            $temp = array();
            $currentMonth = date('Y-m-01 00:00:00',time());
            foreach($result['data'] as &$v){
                $v['item_name'] = $v['checkName'];
                $v['report_date'] = date('Y-m-d', strtotime($v['ReportDate']));
                $v['inspec_no'] = $v['checkId'];
                $v['archive_date'] = $v['checkDate'];
                if($v['ReportDate'] > $currentMonth ){
                    $temp['currentMonth'][] = $v;
                }else{
                    $temp['preMonth'][] =  $v;
                }
            }
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['RespMessage'];
            $returnArr['data'] = array();
        }

        $returnArr['status'] = 1;
        $returnArr['data'] = $temp;
        echo json_encode($returnArr);
        exit;
    }
        
    /**
    * @uses 	检查报告
    * @param 	identy_id=卡号
    * @author 	sunny
    * @since 	2016-5-30
    */
	function myInspectReportResult(){
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        $CheckId=$_POST['exam_no'];

         //获取基础信息
        $params = array();
        $params['patientId'] = '';
        $params['CheckId'] = $CheckId;
        $params['BeginDate'] = '';
        $params['EndDate'] = '';
        $result2= $this->Post(self::$URL_API.'getLis',__FUNCTION__,$params);
        $report_basics=$result2['data'][0];
        if ($report_basics) {
             $data['type']=2;//报告类型
             $data['user_name']=$report_basics['patientName'];//姓名
             $data['exam_item']=$report_basics['checkName'];//检查项目
             $data['exam_no']=$report_basics['checkId'];//检查报告编号
             $data['report_date']=$report_basics['ReportDate'];//报告日期
             $data['check_date']=$report_basics['checkDate'];//检查日期
        }    

        //获取基础信息
        $params = array();
        $params['CheckId'] = $CheckId;
        $params['itemNo'] = '';
        $params['itemName'] = '';
        $result= $this->Post(self::$URL_API.'GetLisDetail',__FUNCTION__,$params);
        if ($result['data']) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = $result['msg'];
            foreach ($result['data'] as $key => $value) {
                $data['item'][]=array(
                        'exam_item'=>$value['itemName'],//项目
                        'diagno_result'=>$value['reportResult'],//结果
                        'unit'=>$value['itemUnit'],//单位
                        'ref_range'=>$value['itemRage'],//参考值
                        'reportDesc'=>$value['reportDesc'],//结果描述
                     );
            }
             $returnArr['data'] = $data;
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['msg'];
            $returnArr['data'] = array();
        }

        echo json_encode($returnArr);
        exit;
    }
    
        
        
    /**
     * @uses 	检验报告列表
     * @param  	identy_id=卡号
     * @author 	sunny
     * @since 	2016-5-31
     */
	public function myExamineReport(){
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        
        //获取基础信息
        $params = array();
        $params['patientId'] = $_POST['cardNo'];
        $params['CheckId'] = '';
        $params['BeginDate'] = '';
        $params['EndDate'] = '';
        $result= $this->Post(self::$URL_API.'getLis',__FUNCTION__,$params);

        if ($result['data']) {
            $temp = array();
            $currentMonth = date('Y-m-01 00:00:00',time());
            foreach($result['data'] as &$v){
                $v['item_name'] = $v['checkName'];
                $v['report_date'] = date('Y-m-d', strtotime($v['ReportDate']));
                $v['inspec_no'] = $v['checkId'];
                $v['archive_date'] = $v['checkDate'];
                if($v['ReportDate'] > $currentMonth ){
                    $temp['currentMonth'][] = $v;
                }else{
                    $temp['preMonth'][] =  $v;
                }
            }
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['RespMessage'];
            $returnArr['data'] = array();
        }

        $returnArr['status'] = 1;
        $returnArr['data'] = $temp;
        echo json_encode($returnArr);
        exit;
	}
        
    /**
     * @uses 	:检验报告
     * @param 	:identy_id=卡号
     * @author 	:sunny
     * @since 	:2016-5-31
     */
	public function myExamineReportResult(){
       $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }
        $CheckId=$_POST['inspec_no'];

         //获取基础信息
        $params = array();
        $params['patientId'] = '';
        $params['CheckId'] = $CheckId;
        $params['BeginDate'] = '';
        $params['EndDate'] = '';
        $result2= $this->Post(self::$URL_API.'getLis',__FUNCTION__,$params);
        $report_basics=$result2['data'][0];
        if ($report_basics) {
             $data['type']=2;//报告类型
             $data['user_name']=$report_basics['patientName'];//姓名
             $data['exam_item']=$report_basics['checkName'];//检查项目
             $data['exam_no']=$report_basics['checkId'];//检查报告编号
             $data['report_date']=$report_basics['ReportDate'];//报告日期
             $data['check_date']=$report_basics['checkDate'];//检查日期
        }    

        //获取基础信息
        $params = array();
        $params['CheckId'] = $CheckId;
        $params['itemNo'] = '';
        $params['itemName'] = '';
        $result= $this->Post(self::$URL_API.'GetLisDetail',__FUNCTION__,$params);
        if ($result['data']) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = $result['msg'];
            foreach ($result['data'] as $key => $value) {
                $data['item'][]=array(
                        'exam_item'=>$value['itemName'],//项目
                        'diagno_result'=>$value['reportResult'],//结果
                        'unit'=>$value['itemUnit'],//单位
                        'ref_range'=>$value['itemRage'],//参考值
                        'reportDesc'=>$value['reportDesc'],//结果描述
                     );
            }
             $returnArr['data'] = $data;
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['msg'];
            $returnArr['data'] = array();
        }

        echo json_encode($returnArr);
        exit;
	}
        
	/**
    * @uses    就诊卡查询和绑定
	* @param   hospitalId  医院ID
    * @param   user_name   病人姓名
    * @param   person_id   身份证号
    * @author  sunny
    * @since   2016-05-19
	*/
	public function cardInfoQuery(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
           echo "签名不正确";exit;
        }
        $user_name = trim($_POST['user_name']);
        $hospitalId = $_POST['hospitalId'];
        $person_id = trim($_POST['person_id']);
        //组装进行访问接口
        $params = [
            'cardNo'        => '' ,         //诊疗卡号
            'IdCode'        => $person_id,  //身份证号
            'patientName'   => $user_name,  //病人姓名
            'patientId'     => '',          //病人编号
        ];
	    $result= $this->Post(self::$URL_API.'getPatientInfo',__FUNCTION__,$params);
        $returnArr['code'] = 0;
        if(trim($result['data'][0]['PatientName']) == $user_name && $user_name){
            $data['identy_id']=$result['data'][0]['IdCard'];
            $data['card_id']=$result['data'][0]['patientId'];
            $data['phone']=$result['data'][0]['phone'];
            $data['sex']=$result['data'][0]['sex'];
            $data['UserIdKey']=$result['data'][0]['cardNo'];
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = '卡号验证成功！';
            $returnArr['data'] = $data;
        }else{
            $returnArr['code'] = 4000;
            $returnArr['errorMsg'] = '卡号验证失败！';
            echo json_encode($returnArr);
            exit;
        }
            echo json_encode($returnArr);
	}


    /**
     * @uses	HIS对账查询
     * @param	user_name=用户名&hospitalId=医院ID&card_id=用户卡号
     * @author	sunny
     * @since  	2016-5-19
     */
    public function BalanceToWeixin(){
        $sign = $this->middelEnCodeSign($_POST);
        if ($sign != $_POST['sign']) {
            echo "签名不正确";
            exit;
        }

        $date=date('Y.m.d',strtotime($_POST['checkDate']));
        $params['date'] = $date;
        $result= $this->Post(self::$URL_API.'bill',__FUNCTION__,$params);
        //数组元素转换
        foreach($result['data'][0]['balances'] as $k=>$v){
            $result['data'][0]['balances'][$k]['transaction_id'] = $v['bank_no'];
        }
        
        if ($result['data']) {
            $returnArr['status'] = 1;
            $returnArr['errorMsg'] = $result['msg'];
            $returnArr['data'] = $result['data'][0]['balances'];
        } else {
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $result['msg'];
            $returnArr['data'] = array();
        }

        echo json_encode($returnArr);
        exit;
    }
    


        
    //获取用户住院缴费信息
    public function getPayInfo(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['identy_id'] = $_POST['identy_id'];
        $data['token'] = $this->token;

        $params = array();
        $params['bs_code'] = 'S_06035';
        $params['reqData'] = json_encode($data);    

        $r = $this->Post(self::$URL_API,__FUNCTION__,$params);

        echo $r;
        exit;
    }
        
    //预交费生成订单
    public function createPayOrder(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
         echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['identy_id'] = $_POST['identy_id'];
        $data['pay_type'] = '30';
        $data['agent_identy_id'] = $_POST['identy_id'];
        $data['pay_fee'] = $_POST['pay_fee'];
        $data['token'] = $this->token;
        $data['pay_channel'] = $this->pay_channel;

        $params = array();
        $params['bs_code'] = 'T_02042';
        $params['reqData'] = json_encode($data);    

        $r = $this->Post(self::$URL_API,__FUNCTION__,$params);

        echo $r;
        exit;
    }
	
    //检查订单是否可以支付
    public function checkOrder(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['identy_id'] = $_POST['identy_id'];
        $data['order_number'] = $_POST['order_number'];
        
        $params = array();
        $params['bs_code'] = 'S_02011';
        $params['reqData'] = json_encode($data);    
        
        $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
    
        echo $r;
        exit;
    }
        
    //提交订单
    public function prePayOrderSubmit(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        //提交订单
        $r = $this->orderSubmit($_POST['order_number'],$_POST['identy_id']);

        echo $r;
        exit;
    }
    
    //医疗机构医生信息
    function deptDoctorList(){
        $sign = $this->middelEnCodeSign($_POST);
        if($sign!=$_POST['sign']){
            echo "签名不正确";exit;
        }
        $hospitalId = $_POST['hospitalId'];
        //组装进行访问接口
        $data['hos_code'] = $this->checkHospital($hospitalId);
        $data['dept_code'] = $_POST['dept_code'];
        $data['c_page'] = $_POST['c_page'];
        
        $params = array();
        $params['bs_code'] = 'S_00104';
        $params['reqData'] = json_encode($data);    
        
        $r = $this->Post(self::$URL_API,__FUNCTION__,$params);
    
        echo $r;
        exit;
    }

    /**
     * @uses 	curl 数据接口访问
     * @param 	$url 数据接口访问地址
     * @param   $method 数据接口访问方法
     * @param  	$post 	数据接口访问参数
     */

    private function Post($url , $method , $post = null) {

        if (is_array($post)) {
            $strIn 	= json_encode($post,JSON_UNESCAPED_UNICODE);
            $ch 	= curl_init($url.'?strIn='.$strIn) ; 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;  
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; 
            $res_f 	= curl_exec($ch) ; 
	        $search = [
	            '<?xml version="1.0" encoding="utf-8"?>'."\r\n".'<string xmlns="http://tempuri.org/">',
	            '</string>',
	            '\'',
	        ];
	        $replace    = ['' , '' , '"'];
	        $res     	= str_replace($search , $replace , $res_f);
            curl_close($ch); 
            if (C('LOG_PATH_TYPE') == 1) {
                $fileContent    = [
                    'hospitalId'=> C('KEY'),
                    'type'      => '2',
                    'params'    => $strIn,
                    'return'    => $res,
                ];
                \Common\Model\LogModel::setLog(json_encode($fileContent));
            } else {
                $fileContent= var_export($post,true)."\r".$res."\r".var_export(json_decode($res,true),true);
                $dir 		= "Log/".C('FOLDER_NAME')."/".C('WEB_LOG_PATH')."/".date("Y-m-d")."/".$method."/";
                $filename 	= date("H_i_s")."_".rand(10000, 99999);
                writeLog($dir, $filename, $fileContent);
            }
            $res=json_decode($res,true);
            return $res;
        }
    }



}

